import {Component, OnInit} from '@angular/core';
import {ApiUrl} from '../../../config/api-url';
import {BaseService} from '../../../shared/service/base.service';
import {Router} from '@angular/router';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';

@Component({
  selector: 'app-system-config',
  templateUrl: './system-config.component.html',
  styleUrls: ['./system-config.component.less']
})
export class SystemConfigComponent implements OnInit {

  isSpinning: any;

  systemConfig: any;

  formModel: FormGroup;

  constructor(private baseService: BaseService, private router: Router, private fb: FormBuilder) {
  }

  ngOnInit(): void {
    this.buildForm();
    this.findSystemConfig();
  }

  buildForm() {
    this.formModel = this.fb.group({
      id: ['', [Validators.required]],
      orderNoticeTime: ['', [Validators.required]],
      onLineInsertCoinNotNoticeTime: ['', [Validators.required]],
      onLineInsertCoinFailRetrySum: ['', [Validators.required]],
      businessStartTime: [null, [Validators.required]],
      businessEndTime: [null, [Validators.required]],
      debugConsumeSum: ['', [Validators.required]],
      unbindSum: ['', [Validators.required]],
      externalDeviceOnLineInsertCoinRecord: ['', [Validators.required]],
      externalDeviceOffLineInsertCoinRecord: ['', [Validators.required]],
      refund: ['', [Validators.required]],
      salesPackageRatio: ['', [Validators.required]],
      salePackageMaximumPrice: ['', [Validators.required]],
      netCardIsOpen: ['', [Validators.required]],
      rebateCondition: [''],
      rebateMoney: [''],
    });
  }


  findSystemConfig() {
    this.baseService.get(ApiUrl.SystemConfigUrl + '/findSystemConfig').subscribe(data => {
      if (data.status == 0) {
        this.systemConfig = data.data;
        const businessStartTimeList = this.systemConfig.businessStartTime.split(':');
        this.systemConfig.businessStartTime = new Date(0, 0, 0, businessStartTimeList[0], businessStartTimeList[1], businessStartTimeList[2]);
        const businessEndTimeList = this.systemConfig.businessEndTime.split(':');
        this.systemConfig.businessEndTime = new Date(0, 0, 0, businessEndTimeList[0], businessEndTimeList[1], businessEndTimeList[2]);
        console.log(this.systemConfig);
        this.formModel.reset(this.systemConfig);
      } else {
        this.baseService.layer.messageError(data.msg);
      }
    });
  }

  handleOk() {
    this.baseService.layer.modalConfirm('确定要更新系统配置吗？', result => {
      if (result) {
        this.isSpinning = true;

        if (!this.baseService.zorro.validateFormValidate(this.formModel)) {
          this.isSpinning = false;
          console.log(1);
          return;
        }

        let param = this.formModel.value;
        let businessStartTime = this.formModel.get('businessStartTime').value;
        let businessEndTime = this.formModel.get('businessEndTime').value;
        param.businessStartTime = this.baseService.zorro.getFormatDateTime(businessStartTime, 'HH:mm:ss');
        param.businessEndTime = this.baseService.zorro.getFormatDateTime(businessEndTime, 'HH:mm:ss');

        this.baseService.post(ApiUrl.SystemConfigUrl + '/update', param).subscribe((res) => {
          if (res.status == 0) {
            this.baseService.layer.messageSuccess('修改成功！');
          } else {
            this.baseService.layer.messageError('修改失败：' + res.msg);
          }
          this.isSpinning = false;
        });
      }
    });
  }
}
